/**
 * 回溯算法
 * @param {number[]} nums
 * @return {number[][]}
 */
var subsets = function (nums) {
  let res = []
  function dfs(index, list) {
    if (index === nums.length) {
      res.push([...list])
      return
    }
    list.push(nums[index])
    dfs(index + 1, list)
    list.pop()
    dfs(index + 1, list)
  }
  dfs(0, [])
  return res
};

/**
 * 迭代算法
 * @param {number[]} nums
 * @return {number[][]}
 */
var subsets = function (nums) {
  let ws = [[]]
  for (let i = 0; i < nums.length; i++) {
    let wsLength = ws.length
    for (let j = 0; j < wsLength; j++ ) {
      ws.push(ws[j].concat([nums[i]]))
    }
  }
  return ws
};

subsets([1,2,3])